import cv2 as cv
import os
import numpy as np



facesPath = './facelib'

modelFileDir='./model/'
modelFileName='face'


names2id = {}
facesSample = []
ids=[]

for index, file in enumerate(os.listdir(facesPath)):
    img = cv.imread(os.path.join(facesPath, file),0)
    facesSample.append(img)
    infos = file.split('-')
    name=infos[0]
    id=names2id.get(name,None)
    if id==None:
        id = len(names2id)
        names2id[name]=id
    ids.append(id)


print(names2id, ids)

recognizer = cv.face.LBPHFaceRecognizer_create()
recognizer.train(facesSample, np.array(ids))

recognizer.write(os.path.join(modelFileDir,modelFileName)+'.model')

id2name=dict([val, key] for key, val in names2id.items())
print(id2name)
np.save(os.path.join(modelFileDir,modelFileName),id2name)
